//
//  SpeedDialViewController.swift
//  DoctorVisits
//
//  Created by lanou3g on 16/2/29.
//  Copyright © 2016年 SHH. All rights reserved.
//

import UIKit

class SpeedDialViewController: UIViewController {
    
    let urlString = "http://api.le123.com/kuaikan/apilistnew_json.so?city=&pageindex=1&orderby=3&plattype=iphone&pagesize=18&uuid=53e0129ce38e53e8eeefdb7db67948ba7b04f821_53e0129ce38e53e8eeefdb7db67948ba7b04f821&lc=53e0129ce38e53e8eeefdb7db67948ba7b04f821&version=1.7.1&code=ef14fd20217b550c&platform=Le123Plat001&vt="
    
    //屏幕的宽和高
    let width = UIScreen.mainScreen().bounds.width
    let height = UIScreen.mainScreen().bounds.height
    
    var numB:String?
    
    var layout:UICollectionViewFlowLayout!
    var collectionView:UICollectionView! //初始化collectionView
    
    var dataSecondArray = NSArray()
    
    let filter:FilterView = FilterView()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.title = "电视剧"
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "筛选", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("fiterAction"))
        //加载UICollectionView
        loadCollectionView()
        dataParsing()
    }
    
    func dataParsing(){
        
        let urlStr = urlString+numB!
        print(urlStr)
        ChannelDataHelper.shareInstance.requestSecondModelWithUrlString(urlStr) { (array:Array<ChannelSecondViewModel>) -> Void in
            
            self.dataSecondArray = array
            
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.collectionView.reloadData()
            })
        }
    }
    
    
    func fiterAction() ->Void{
        
        print("出来")
        self.view.frame = CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "筛选", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("backAction"))
        
        filter.frame = CGRectMake(0, -self.view.frame.size.height*0.2, self.view.frame.size.width,0)
        self.view.addSubview(filter)
        
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(0.3)
        UIView.setAnimationDelegate(self)
        self.view.frame = CGRectMake(0, self.view.frame.size.height*0.3, self.view.frame.size.width, self.view.frame.size.height)
        filter.frame = CGRectMake(0, -self.view.frame.size.height*0.2, self.view.frame.size.width, self.view.frame.size.height*0.28)
        self.view.addSubview(filter)
        UIView.commitAnimations()
        
    }
    
    func backAction() ->Void{
        print("回去")
        //           self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
        
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            UIView.beginAnimations(nil, context: nil)
            UIView.setAnimationDuration(0.01)
            UIView.setAnimationDelegate(self)
            
            self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
            //            self.filter.frame = CGRectMake(0, -self.view.frame.size.height*0.2, self.view.frame.size.width,0)
            self.filter.frame = CGRectMake(0, -self.view.frame.size.height*0.2, self.view.frame.size.width,0)
            
            self.view.addSubview(self.filter)
            UIView.commitAnimations()
            
            self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "筛选", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("fiterAction"))
            
        }
        
        self.filter.removeFromSuperview()
        
        
        //        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(3 * NSEC_PER_SEC)), dispatch_get_main_queue()) { () -> Void in
        //
        //        }
        
        
    }
    
    func loadCollectionView(){
        //定义collectionView的布局类型，流布局
        let layout = UICollectionViewFlowLayout()
        //设置cell的大小
        layout.sectionInset = UIEdgeInsetsMake(0,  self.view.frame.size.width*0.03, 5, self.view.frame.size.width*0.03)
        layout.itemSize = CGSize(width: self.view.frame.size.width*0.3, height:self.view.frame.size.height*0.29)
        //滑动方向 默认方向是垂直
        layout.scrollDirection = .Vertical
        //每个Item之间最小的间距
        layout.minimumInteritemSpacing = 0
        //每行之间最小的间距
        layout.minimumLineSpacing = 10
        //定义一个UICollectionView
        collectionView = UICollectionView(frame: CGRectMake(0,0,width,height), collectionViewLayout: layout)
        //设置collectionView的背景颜色
        collectionView.backgroundColor = UIColor.groupTableViewBackgroundColor()
        //        collectionView.alpha = 0.5
        //设置collectionView的代理和数据源
        collectionView.delegate = self
        collectionView.dataSource = self
        //CollectionViewCell的注册
        collectionView.registerNib(UINib(nibName: "SpeedDialCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell")
        //header的注册
        collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headView")
        
        self.view.addSubview(collectionView)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
}

extension SpeedDialViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
    // #MARK: --UICollectionViewDataSource的代理方法
    /**
    - 该方法是可选方法，默认为1
    - returns: CollectionView中section的个数
    */
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    
    /**
     - returns: Section中Item的个数
     */
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ChannelDataHelper.shareInstance.dataArray2.count
    }
    
    /**
     - returns: 绘制collectionView的cell
     */
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! SpeedDialCollectionViewCell
        
        let secondModel:ChannelSecondViewModel = self.dataSecondArray[indexPath.row] as! ChannelSecondViewModel
        
        
        cell.nameLabel.text = secondModel.name
        cell.numLabel.text = secondModel.nowepisodes
        let url = NSURL(string: secondModel.poster!)
        cell.imageV.sd_setImageWithURL(url, placeholderImage: UIImage(named: "picholder@2x"))
        
        return cell
    }
    
    /**
     - returns: 返回headview或者footview
     */
     //    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
     //        let headView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "headView", forIndexPath: indexPath)
     //        headView.backgroundColor = UIColor.whiteColor()
     //
     //        return headView
     //    }
     
     // #MARK: --UICollectionViewDelegate的代理方法
     /**
     Description:当点击某个Item之后的回应
     */
    
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        let channelInfo:ChannelShowController = ChannelShowController()
        self.navigationController?.pushViewController(channelInfo, animated: true)
        
        
    }
    
    
    //#MARK: --UICollectionViewDelegateFlowLayout的代理方法
    /**
    - returns: header的大小
    */
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: width, height: 5)
    }
    /**
    Description:可以定制不同的item
    
    - returns: item的大小
    */
    //    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    //        if  indexPath.row % 2 == 1{
    //            return CGSize(width: width/2, height: height/3)
    //        }
    //        else{
    //            return CGSize(width: width/2, height: height/2)
    //        }
    //    }
}
